package com.mayavi.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.mayavi.bo.CelebrityBo;
import com.mayavi.bo.CelebrityTypeBo;
import com.mayavi.criteria.CelebrityTypeCriteria;
import com.mayavi.dao.ICelebrityDao;

public class CelebrityDaoImpl implements ICelebrityDao{
	private HibernateTemplate hibernateTemplate;
	
	public List<CelebrityBo> getAllCelebrities() {
		return hibernateTemplate.find("from CelebrityBo");
	}
	
	public List<CelebrityTypeBo> getAllCelebrityTypes() {
		return hibernateTemplate.find("from CelebrityTypeBo");
	}
	
	public CelebrityBo getCelebrityById(Integer id){
		return (CelebrityBo)hibernateTemplate.get(CelebrityBo.class, id);
	}
	
	
	public void saveCelebrity(CelebrityBo celebrityBo){
		hibernateTemplate.saveOrUpdate(celebrityBo);
	}
	
	public List<CelebrityTypeBo> getCelebrityTypesByCriteria(CelebrityTypeCriteria celebrityTypeCriteria){
		Criteria crit = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(CelebrityTypeBo.class);
		if(celebrityTypeCriteria.getIds()!=null&&celebrityTypeCriteria.getIds().length>0){
			crit.add(Restrictions.in("id", celebrityTypeCriteria.getIds()));
		}
		if(celebrityTypeCriteria.getExcludeIds()!=null&&celebrityTypeCriteria.getExcludeIds().length>0){
			crit.add(Restrictions.not(Restrictions.in("id", celebrityTypeCriteria.getExcludeIds())));
		}
		return (List<CelebrityTypeBo>)crit.list();

	}
	
	
	public void setSessionFactory(SessionFactory sessionFactory){
		this.hibernateTemplate = new HibernateTemplate(sessionFactory);
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
}
